package com.yungam.other.mes_controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.gson.Gson;
import com.yungam.common.bean.user.User;
import com.yungam.common.service.UserService;
import com.yungam.other.response.ResData;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 用户信息同步接口
 */
@Slf4j
@RestController
public class SynchronizeUserController {

    @Resource
    private UserService userService;

    @PostMapping("/IMCS/synchronizeUser")
    public Map<String, Object> synchronizeUser(@RequestBody List<Map<String, String>> params) {
        log.info("========= START『mes调用imcs』[用户信息同步接口]=========");
        log.info("=========『mes数据』{}", new Gson().toJson(params));

        // 先删后插入
        userService.remove(new QueryWrapper<User>().eq("user_type", 0));
        for (Map<String, String> map : params) {
            User user = new User();
            user.setUserName(map.get("accountNo"));
            user.setUserType(0);
            user.setName(map.get("name"));
            user.setCreateTime(new Date());
            userService.save(user);
        }

        log.info("同步成功");
        return ResData.success("同步成功");
    }

}
